package com.endoc.livedatabuslearn;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.jeremyliao.liveeventbus.LiveEventBus;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final LiveDataViewModel instance = LiveDataViewModel.getInstance(this);
        findViewById(R.id.tv_test).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

                 //TODO 发送String类型,然后key为"String.class_key"
                 LiveEventBus
                         .get("String.class_key")
                         .post("123");


                 instance.person =new Person();
                 instance.person.name ="宋炯乐";
                 Log.d(StringDataUitls.LiveDataBus,"MainActivity:    "+instance.toString());

                 //TODO 发送自定义Person类,Person类需要继承LiveEvent
                 LiveEventBus
                         .get(String.valueOf(instance),Person.class)
                         .post(instance.person);

                 startActivity(new Intent(MainActivity.this,Main2Activity.class));
                 //Log.d(StringDataUitls.LiveDataBus,"发送");

             }
         });

        LiveEventBus
                .get(instance.data, String.class)
                .observe(this, new Observer<String>() {
                    @Override
                    public void onChanged(@Nullable String s) {
                        Log.d(StringDataUitls.LiveDataBus,"MainActivity:    "+s);
                        //Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
                    }
                });
    }
}
